package com.RLFZ.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.RLFZ.entity.HealthClock;
import com.RLFZ.entity.User;
import com.RLFZ.service.HealthClockService;
import com.RLFZ.vo.DataView;
import com.RLFZ.vo.HealthClockVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author 86166
 */
@Slf4j
@RequiredArgsConstructor
@Controller
public class OneselfHealthClockController {
    @Autowired
    private HealthClockService healthClockService;
    @Autowired
    private HttpSession session;
    @RequestMapping("/toOneselfHealthClock")
    public String toOneselfHealthClock(){
        return "admin/oneSelfHealthclock";
    }

    /**
     * 查询自己的打卡记录 带有模糊查询的条件 带有分页
     * @param healthClockVo
     * @return
     */
    @RequestMapping("/oneSelfListHealthClock")
    @ResponseBody
    public DataView listHealthClock(HealthClockVo healthClockVo){
        // 查询所有带有模糊查询的条件 带有分页
        User user = (User)session.getAttribute("user");
        String name = user.getName();
        QueryWrapper<HealthClock> queryWrapper=new QueryWrapper<>();
        // 查name为当前用户的健康打卡记录
        QueryWrapper<HealthClock> wrapper = queryWrapper.like("name", name);
        wrapper.like(StringUtils.isNoneBlank(healthClockVo.getName()),"name",healthClockVo.getName());
        wrapper.eq(StringUtils.isNoneBlank(healthClockVo.getPhone()),"phone",healthClockVo.getPhone());
//        queryWrapper.like(healthClockVo.getUsername()!=null,"username",healthClockVo.getUsername());
//        queryWrapper.eq(healthClockVo.getPhone()!=null,"phone",healthClockVo.getPhone());
        IPage<HealthClock> page=new Page<>(healthClockVo.getPage(),healthClockVo.getLimit());
        healthClockService.page(page,wrapper);

        
//        queryWrapper.like(StringUtils.isNoneBlank(userVo.getUsername()),"username",userVo.getUsername());
//        queryWrapper.eq(StringUtils.isNoneBlank(userVo.getPhone()),"phone",userVo.getPhone());
        // 查询所有带有模糊查询的条件 带有分页
        return  new DataView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加或更改数据
     * @param healthClock
     * @return
     */
    @RequestMapping("/addOneSelfHealthClock")
    @ResponseBody
    public DataView addHealthClock(HealthClock healthClock){
//        User user = (User)session.getAttribute("user");
        boolean b = healthClockService.saveOrUpdate(healthClock);
        DataView dataView = new DataView();
        if (b){
            dataView.setCode(200);
            dataView.setMsg("添加成功!");
        }else {
            dataView.setCode(100);
            dataView.setMsg("添加失败");
        }
        return dataView;
    }

//    /**
//     * 删除成功
//     * @param id
//     * @return
//     */
//    @RequestMapping("/deleteHealthClockById")
//    @ResponseBody
//    public DataView deleteHealthClockById(Integer id){
//        healthClockService.removeById(id);
//        DataView dataView = new DataView();
//        dataView.setCode(200);
//        dataView.setMsg("删除数据成功!");
//        return dataView;
//    }

    /**
     * 批量删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteListSelfHealthClock")
    @ResponseBody
    public DataView deleteHealthClockById(Integer[] id){
    System.out.println(id[0]+"------------");
        for (int i=0;i<id.length;i++){
            healthClockService.removeById(id[i]);
        }
    DataView dataView = new DataView();
    dataView.setCode(200);
    dataView.setMsg("删除数据成功!");
    return dataView;
    }



}
